#!/bin/bash

# 设置工作目录
WORK_DIR="/root/telltech"
cd $WORK_DIR

# 激活conda环境
source /root/anaconda3/etc/profile.d/conda.sh
conda activate telltech

# 获取conda环境中的python路径
PYTHON_PATH=$(which python)
echo "Using Python from: $PYTHON_PATH"

# 启动后端
echo "Starting backend service..."
cd $WORK_DIR
nohup $PYTHON_PATH backend/run.py -p > backend.log 2>&1 &
BACKEND_PID=$!

# 检查后端是否成功启动
sleep 5
if ! ps -p $BACKEND_PID > /dev/null; then
    echo "Error: Backend failed to start. Check backend.log for details."
    exit 1
fi

echo "Backend started with PID: $BACKEND_PID"

# 启动前端
echo "Starting frontend service..."
cd $WORK_DIR/frontend
nohup npm run start:p > frontend.log 2>&1 &
FRONTEND_PID=$!

# 检查前端是否成功启动
sleep 5
if ! ps -p $FRONTEND_PID > /dev/null; then
    echo "Error: Frontend failed to start. Check frontend.log for details."
    kill $BACKEND_PID
    exit 1
fi

echo "Frontend started with PID: $FRONTEND_PID"

# 保存PID到文件
echo $BACKEND_PID > $WORK_DIR/backend.pid
echo $FRONTEND_PID > $WORK_DIR/frontend.pid

echo "Services started successfully!"
echo "Backend log: $WORK_DIR/backend.log"
echo "Frontend log: $WORK_DIR/frontend.log" 